package com.cole.miniapp.project.notify.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaMessage;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import com.cole.miniapp.common.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 接收微信推送过来的消息
 *
 * @author cole
 *
 */
@RestController
@RequestMapping("/wx/portal")
public class WxPortalController {

	private static final Logger log = LoggerFactory.getLogger(WxPortalController.class);

	@Autowired
	WxMaService WxMaService;

	@GetMapping(produces = "text/plain;charset=utf-8")
	public String authGet(@RequestParam(name = "signature", required = false) String signature,
			@RequestParam(name = "timestamp", required = false) String timestamp,
			@RequestParam(name = "nonce", required = false) String nonce,
			@RequestParam(name = "echostr", required = false) String echostr) {
		log.info("接收到来自微信服务器的认证消息：signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]", signature,
				timestamp, nonce, echostr);

		if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
			throw new IllegalArgumentException("请求参数非法，请核实!");
		}

		if (WxMaService.checkSignature(timestamp, nonce, signature)) {
			return echostr;
		}

		return "非法请求";
	}

	@PostMapping(produces = "application/xml; charset=UTF-8")
	public String post(@RequestBody String requestBody,
			@RequestParam(name = "msg_signature", required = false) String msgSignature,
			@RequestParam(name = "encrypt_type", required = false) String encryptType,
			@RequestParam(name = "signature", required = false) String signature,
			@RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce) {
		log.info(
				"接收微信请求：[msg_signature=[{}], encrypt_type=[{}], signature=[{}],"
						+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
				msgSignature, encryptType, signature, timestamp, nonce, requestBody);

		final boolean isJson = Objects.equals(WxMaService.getWxMaConfig().getMsgDataFormat(),
				WxMaConstants.MsgDataFormat.JSON);

		// if (StringUtils.isBlank(encryptType)) {
		// 明文传输的消息
		// WxMaMessage inMessage;
		// if (isJson) {
		// inMessage = WxMaMessage.fromJson(requestBody);
		// } else {// xml
		// inMessage = WxMaMessage.fromXml(requestBody);
		// }
		// 加上针对具体对应消息做相应处理

		// return "success";
		// }

		// if ("aes".equals(encryptType)) {
		// 是aes加密的消息
		WxMaMessage inMessage;
		if (isJson) {
			inMessage = WxMaMessage.fromEncryptedJson(requestBody, WxMaService.getWxMaConfig());
		} else {// xml
			inMessage = WxMaMessage.fromEncryptedXml(requestBody, WxMaService.getWxMaConfig(), timestamp, nonce,
					msgSignature);
		}
		log.info("接收微信消息解码结果：[{}]",JsonUtils.objectToJson(inMessage));
		// 加上针对具体对应消息做相应处理

		return "success";
		// }

		// throw new IllegalArgumentException("不可识别的加密类型：" + encryptType);
	}
}
